package com.hj.flower;

import java.util.Scanner;

public class FlowerIndex {
    public static final FlowerOrder[] orderList = new FlowerOrder[5];

    static {

        orderList[0] = new FlowerOrder("1001", "金色海岸-香槟玫瑰", 10, 318, "2016-12-12", "王二昵", "");
        orderList[1] = new FlowerOrder("1002", "初心永不变-白玫瑰", 99, 538, "2016-12-20", "张欣雨", "");
        orderList[2] = new FlowerOrder("1003", "梦里花开-紫色桔梗", 40, 148, "2016-12-24", "王二昵", "眼波如水，映出窗外繁星点点");
        orderList[3] = new FlowerOrder("1004", "心花怒放-纷香百合", 120, 188, "2016-12-26", "王二昵", "内层银灰色厚绵纸，外层深蓝色条纹纸，银灰色缎带");
        orderList[4] = new FlowerOrder("1005", "致青春-无声的爱", 1000, 219, "2016-12-18", "张欣雨", "我爱你，胜过爱爱情，更胜过爱自己！");
    }

    public static void main(String[] args) {
        outer:
        while (true) {
            System.out.println("===========欢迎光临\"七彩鲜花\"销售管理系统==========");
            System.out.println("1.查询销售订单");
            System.out.println("2.修改销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.系统退出");
            System.out.println("================================================");
            System.out.println("请输入编号：");
            int choice = new Scanner(System.in).nextInt();
            switch (choice) {
                case 1:
                    finAll();
                    break;
                case 2:
                    change();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    System.out.println("退出系统");
                    break outer;
                default:
                    System.err.println("输入有误");
                    break;
            }
        }
    }

    public static void finAll() {
        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        for (FlowerOrder flowerOrder : orderList) {
            System.out.println(flowerOrder.getId() + "\t" + flowerOrder.getName() + "\t" + flowerOrder.getNum() + "\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getSalEmp() + "\t" + flowerOrder.getBeiZhu());
            System.out.println();
        }
    }

    public static void change() {
        System.out.println("请输入销售编号：");
        String id = new Scanner(System.in).next();
        boolean flag = false;
        for (FlowerOrder flowerOrder : orderList) {
            if (flowerOrder.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("您要修改的订单详细信息如下：");
            System.out.println();
            System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
            for (FlowerOrder flowerOrder : orderList) {
                if (flowerOrder.getId().equals(id)) {
                    System.out.println(flowerOrder.getId() + "\t" + flowerOrder.getName() + "\t" + flowerOrder.getNum() + "\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getSalEmp() + "\t" + flowerOrder.getBeiZhu());
                    System.out.println();
                }
            }
            System.out.println("请输入修改后的鲜花名称：");
            String newName = new Scanner(System.in).next();
            System.out.println("请输入修改后的销售数量：");
            int newNum = new Scanner(System.in).nextInt();
            System.out.println("请输入修改后的鲜花价格：");
            double newPrice = new Scanner(System.in).nextDouble();
            System.out.println("请输入修改后的销售日期：（yyy-MM-dd）");
            String newDate = new Scanner(System.in).next();
            System.out.println("请输入修改后的销售员：");
            String newSalEmp = new Scanner(System.in).next();
            System.out.println("请输入修改后的备注：");
            String newBeiZhu = new Scanner(System.in).next();
            FlowerOrder order = new FlowerOrder(newName, newNum, newPrice, newDate, newSalEmp, newBeiZhu);
            System.out.println("您确定要修改吗？（y/n）");
            String choice = new Scanner(System.in).next();
            if ("y".equals(choice) || "Y".equals(choice)) {
                for (int i = 0; i < orderList.length; i++) {
                    if (orderList[i].getId().equals(id)) {
                        orderList[i] = order;
                    }
                }
                System.out.println("修改成功");
                System.out.println();
                finAll();
            }
        } else {
            System.err.println("输入的编号不存在");
        }
    }

    public static void delete() {
        System.out.println("请输入销售编号：");
        String id = new Scanner(System.in).next();
        boolean flag = false;
        for (FlowerOrder flowerOrder : orderList) {
            if (flowerOrder.getId().equals(id)) {
                flag = true;
                break;
            }
        }

        if (flag) {
            System.out.println("您要删除的订单详细信息如下：");
            System.out.print("\n");
            System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
            for (FlowerOrder flowerOrder : orderList) {
                if (flowerOrder.getId().equals(id)) {
                    System.out.println(flowerOrder.getId() + "\t" + flowerOrder.getName() + "\t" + flowerOrder.getNum() + "\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getSalEmp() + "\t" + flowerOrder.getBeiZhu());
                    System.out.println();
                }
            }
            System.out.println("确定要删除吗？（y/n）");
            String choice = new Scanner(System.in).next();
            if ("y".equals(choice) || "Y".equals(choice)) {
                FlowerOrder[] newOrderList = new FlowerOrder[4];
                for (int i = 0, j = 0; i < orderList.length; i++) {
                    if (orderList[i].getId().equals(id)) {
                        continue;
                    }
                    newOrderList[j] = orderList[i];
                    j++;
                }
                System.out.println("删除成功！");
                System.out.println();
                System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
                for (FlowerOrder flowerOrder : newOrderList) {
                    System.out.println(flowerOrder.getId() + "\t" + flowerOrder.getName() + "\t" + flowerOrder.getNum() + "\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getSalEmp() + "\t" + flowerOrder.getBeiZhu());
                    System.out.println();
                }
            } else {
                System.err.println("输入有误");
            }
        }
    }
}